package com.thumbnilatorDemo;

import net.coobird.thumbnailator.Thumbnails;
import java.io.*;

public class ImageCompressorStream {

    public static byte[] compressImageBytes(byte[] inputBytes, int maxWidth, float quality) throws IOException {
        try (ByteArrayInputStream in = new ByteArrayInputStream(inputBytes);
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {

            Thumbnails.of(in)
                    .width(maxWidth)
                    .outputQuality(quality)
                    .outputFormat("jpg")
                    .toOutputStream(out);

            return out.toByteArray();
        }
    }
}
